package com.qianfeng.day11.service;

import com.qianfeng.day11.dao.PhoneDAO;
import com.qianfeng.day11.entity.LineChartEntity;
import com.qianfeng.day11.entity.Phone;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class PhoneService {
    @Resource
    private PhoneDAO phoneDAO;

//    public List<Phone> findAll(){
//        return phoneDAO.findAll();
//    }

    public List<Phone> findAll(Integer typeId, String key){
        if (key != null){
            if (key.trim().equals("")){
                key = null;
            }else {
                key = "%" + key + "%";
            }
        }
        return phoneDAO.findAll(typeId, key);
    }

    public Phone findById(Integer id){
        return phoneDAO.findById(id);
    }

    public Double findPriceLast30Days(Integer id){
        return phoneDAO.findPriceLast30Days(id);
    }

    public List<LineChartEntity> findPriceLast4Months(Integer id){
        List<LineChartEntity> list = phoneDAO.findPriceLast4Months(id);
        Collections.reverse(list);
        return list;
    }
 }
